C     MAIN     
C     Copyright (C) 1997  Filip Hroch, Masaryk University, Brno, CZ
C                    
C     This program is free software; you can redistribute it and/or modify
C     it under the terms of the GNU General Public License as published by
C     the Free Software Foundation; either version 2 of the License, or
C     (at your option) any later version.
C     
C     This program is distributed in the hope that it will be useful,
C     but WITHOUT ANY WARRANTY; without even the implied warranty of
C     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
C     GNU General Public License for more details.
C
C     You should have received a copy of the GNU General Public License  
C     along with this program; if not, write to the Free Software
C     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
C
C    E-mail: hroch@physics.muni.cz
C    Web:    http://www.sci.muni.cz/~hroch/index.html
C    Postal address: F.Hroch
C                    Department of Theoretical Physics and Astrophysics
C                    Masaryk University
C                    Kotlarska 2
C                    611 37 Brno
C                    Czech Republic
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C                                                                      C
C !!!!!!!!!!!!!!!!     This is DOS version   !!!!!!!!!!!!!!!!!!!!!!!!! C
C                                                                      C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C For Microsoft Powerstation compiler

      INCLUDE 'FLIB.FI'
      INCLUDE 'FLIB.FD'

C      PROGRAM MUNIMAT
C Find cross-references between CCD frames or between CCD frame 
C and catalogue.

      INTEGER I,J
      CHARACTER*80 NAMSCI,REF,LINE
      CHARACTER*10 PARY
      INTEGER*2 S
      LOGICAL STDINP

      DATA STDINP/.FALSE./,REF/' '/,NAMSCI/' '/,PARY/' '/

      PRINT *,'MUNIMATCH Version 1.11, Copyright (C) 1997 F.Hroch,',
     *        'Masaryk University, Brno, CZ'
      PRINT *,'Munimatch comes with ABSOLUTELY NO WARRANTY;',
     *        'This is free software, and you are'
      PRINT *,'welcome to redistribute under certain conditions;',
     *        'see "gnulic.doc" for details.'
      PRINT *

      DO I = 1,NARGS()-1
        CALL GETARG(INT2(I),LINE,S)
C dir file
        J = INDEX(LINE,'@')
        IF( J.NE.0 )THEN
          NAMSCI = LINE(J+1:)
          IF( NAMSCI.EQ.' ' ) STDINP = .TRUE.
        ENDIF
C reference file
        J = INDEX(LINE,'ref=')
        IF( J.NE.0 ) REF = LINE(J+4:)
C spherical coordinates refrence file
        J = INDEX(LINE,'cat=')
        IF( J.NE.0 )THEN 
          REF = LINE(J+4:)
          PARY(1:1) = 'C'
        ENDIF
        IF( INDEX(LINE,'-k').NE.0 ) PARY(2:2) = 'K'
      ENDDO

      IF( REF.EQ.' ' .AND. NARGS().GT.1 )
     *   STOP 'Missed name of reference file.'

C Input from file. Open file with single files.
      IF( NAMSCI .NE. ' ')THEN
        OPEN(4,FILE=NAMSCI,STATUS='OLD',IOSTAT=I)
        IF(I.NE.0) STOP 'Can''t open file with name(s) of catalogue(s).'
C Loop over all frames.
10      READ(4,'(A)',END=20) LINE
          IF( LINE.EQ.' ' ) GOTO 10
          PRINT *
          PRINT *,('*',I=1,78)
          PRINT *
          CALL TRI(REF,LINE,PARY)
        GOTO 10
20      CLOSE(4)
      ENDIF

C image names read from command line
      DO I = 1, NARGS()-1
        CALL GETARG(INT2(I),LINE,S)
        IF( INDEX(LINE,'=').EQ.0 .AND. INDEX(LINE,'@').EQ.0 .AND.
     +      INDEX(LINE,'-').EQ.0  ) THEN
          PRINT *
          PRINT *,('*',J=1,78)
          PRINT *
          CALL TRI(REF,LINE,PARY)
        ENDIF
      ENDDO

C images names from standart input
      IF( STDINP )THEN
40      READ(*,'(A)',END=50) LINE
          PRINT *
          PRINT *,('*',I=1,78)
          PRINT *
          CALL TRI(REF,LINE,PARY)
        GOTO 40
50      CONTINUE
      ENDIF

      IF( NARGS().EQ.1 )THEN
        PRINT *,'MUNIMATCH   Find cross-references between catalogues.'
        PRINT *
        PRINT *,'Usage:munimat [@dirfile] ref=refcat [cat1] ...'
        PRINT *
        PRINT *,'@dirfile= file with name(s) of stars catalogue(s)'
        PRINT *,'          (for only "@", read catalogue(s) name(s) from
     * standart input)'
        PRINT *,'refcat= reference catalogue'
        PRINT *,'cat1...= name(s) of catalogue(s)'
        PRINT *
        PRINT *,'"Catalogue" is file with numbers and coordinates of sta
     *rs (in free format)'
        PRINT *,'or any output file from DAOPHOT (MUNIPHOT).'
        PRINT *,'Example of catalogue:'
        PRINT *,' 1 10.1 20.2'
        PRINT *,' 2 30.3 40.4'
        PRINT *,' 3 50.5 60.6'
        PRINT *
        PRINT *,'You can sort catalogue according to magnitudes of stars 
     * before MUNIMAT run.' 
        PRINT *,'MUNIMAT will work probable most successfully.'
      ENDIF

      END      
      
